package engine.controllers;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import engine.entities.Entity;
import engine.entities.Spatial;

public class Keyboard extends KeyAdapter implements Controller {
	private final int UP = 0;
	private final int DOWN = 1;
	private final int LEFT = 2;
	private final int RIGHT = 3;
	boolean pressed[] = new boolean[4];

	public Keyboard() {
		for(int i = 0; i < pressed.length ; i++){
			pressed[i] = false;
		}
	}

	@Override
	public void updateController(Entity e,float delta) {
		Spatial spatial = e.getSpatial();
		int mass = e.getPhysical().mass;
		if(pressed[UP] && spatial.getY() == 0 )
			spatial.addSpeedY(500*delta);
		else if(pressed[DOWN])
			spatial.addSpeedY(-5*delta);
		if(pressed[LEFT])
			spatial.addSpeedX(-10*delta);
		else if(pressed[RIGHT])
			spatial.addSpeedX(10*delta);
		else if(spatial.getY() == 0)
			spatial.addSpeedX(-spatial.getSpeedX()*delta*4);
		else
			spatial.addSpeedX(-spatial.getSpeedX()*delta*4);
	}

	@Override
	public void keyPressed(KeyEvent e) {
		int lastKeyCode = e.getKeyCode();
		switch (lastKeyCode) {
		case KeyEvent.VK_W:
				pressed[UP] = true;
			break;
		case KeyEvent.VK_A:
				pressed[LEFT] = true;
			break;
		case KeyEvent.VK_S:
				pressed[DOWN] = true;
			break;
		case KeyEvent.VK_D:
				pressed[RIGHT] = true;
			break;
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
		int lastKeyCode = e.getKeyCode();
		switch (lastKeyCode) {
		case KeyEvent.VK_W:
				pressed[UP] = false;
			break;
		case KeyEvent.VK_A:
				pressed[LEFT] = false;
			break;
		case KeyEvent.VK_S:
				pressed[DOWN] = false;
			break;
		case KeyEvent.VK_D:
				pressed[RIGHT] = false;
			break;
		}
	}

}
